package com.tech.koufu.ui.view;

import java.util.ArrayList; 

import com.tech.koufu.R;
import com.tech.koufu.action.StockManager;
import com.tech.koufu.model.Stock;
import com.tech.koufu.model.UserStocks;
import com.tech.koufu.ui.activity.BuyStocksActivity;
import com.tech.koufu.ui.adapter.QueryStockListAdapter;
import com.tech.koufu.ui.adapter.StocksGridPagerAdapter;

import android.content.Intent;
import android.os.Bundle; 
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Editable;
import android.text.TextWatcher; 
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener; 
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView; 
import android.widget.ListView;
import android.widget.TextView; 

/**
 * 选股页面
 * @author jl_luo
 *
 */
public class ChooseStockFragment extends FragmentActivity implements OnClickListener,OnPageChangeListener{

	private TextView tv_title,chooseTitle;
	private ImageView img_callback;
	private ViewPager vp;
	private StocksGridPagerAdapter mAdapter;
	private ImageView mIvLeft,mIvRight; 
	private String[] tabs = new String[]{"自选股票","热门股票"};
	private EditText edSearchStock;
	private ImageView ivSearch;
	private int position = 0;
//	private LinearLayout ll_pager;
	private ListView lv_search;
	private String key;
	private StockManager mStockManager;
	private Handler mHandler;
	ArrayList<Stock> lst = null;
	private Stock queryStock = null;
//	private ArrayList<UserStocks> datas;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_choose_stock);
		initView();
	}
	
	private void initView(){
//		datas = (ArrayList<UserStocks>) getIntent().getSerializableExtra("userStock");
//		ll_pager = (LinearLayout)findViewById(R.id.ll_pager);
		lv_search = (ListView)findViewById(R.id.lv_search);
		edSearchStock = (EditText) findViewById(R.id.ed_serch_stock);
		ivSearch = (ImageView)findViewById(R.id.img_serch_stock);
		ivSearch.setOnClickListener(this);
		tv_title = (TextView)findViewById(R.id.tv_title);
		tv_title.setText(R.string.head_choose_stocks);
		tv_title.setVisibility(View.VISIBLE);
		img_callback = (ImageView)findViewById(R.id.img_callback);
		img_callback.setVisibility(View.VISIBLE);
		img_callback.setOnClickListener(this);
		mIvLeft = (ImageView)findViewById(R.id.img_cs_toleft);
		mIvRight = (ImageView)findViewById(R.id.img_cs_toright);
		mIvLeft.setOnClickListener(this);
		mIvRight.setOnClickListener(this);
		vp = (ViewPager)findViewById(R.id.vp_cs);
		chooseTitle = (TextView)findViewById(R.id.tv_cs_title);
		chooseTitle.setText(tabs[0]);
		mAdapter = new StocksGridPagerAdapter(getSupportFragmentManager());
//		mAdapter.datas = datas;
		vp.setAdapter(mAdapter);
		vp.setOnPageChangeListener(this);
		initEvent();
	}
	 
	private void initEvent(){
		mHandler = new Handler(){
			@Override
			public void handleMessage(Message msg) { 
				super.handleMessage(msg);
			}
		};
		mStockManager = new StockManager(getApplicationContext(), getApplication(), mHandler);
		edSearchStock.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {  
				key =  edSearchStock.getText().toString();  
				 if(key!=null && !"".equals(key.trim())){  
					 lst = mStockManager.findStocks(key.trim());
					 QueryStockListAdapter queryStockListAdapter = new QueryStockListAdapter(getApplicationContext(), lst);
					 lv_search.setAdapter(queryStockListAdapter);
					 lv_search.setOnItemClickListener(queryItemClick);
					 lv_search.setVisibility(View.VISIBLE);
				 }else{
					 lv_search.setAdapter(null);
					 lv_search.setVisibility(View.GONE);
				 }
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) { 
				
			}
			
			@Override
			public void afterTextChanged(Editable s) { 
				
			}
		});
	}
	@Override
	public void onClick(View v) { 
		switch (v.getId()) {
		case R.id.img_callback:
			this.finish();
			break;
		case R.id.img_cs_toleft:
			if(position==1){
				vp.setCurrentItem(0);
			}
			break;
		case R.id.img_cs_toright:
			if(position==0){
				vp.setCurrentItem(1);
			}else{
//				Toast.makeText(context, text, duration)
			}
			break;
		case R.id.img_serch_stock:
			if(lst!=null && queryStock!=null){
				notifySearch(key,queryStock);
			}
			break;

		default:
			break;
		}
	}
	private OnItemClickListener queryItemClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			queryStock = lst.get(position);
//			String text = stock.name+"("+stock.code+")";
//			edSearchStock.setText(queryStock.name+"("+queryStock.code+")");
//			notifySearch(key,queryStock);
			Intent intent = new Intent(ChooseStockFragment.this, BuyStocksActivity.class);
//			intent.putExtra("stockInfo", datas.get(position));
			intent.putExtra("code", queryStock.code);
			intent.putExtra("name", queryStock.name);
			intent.putExtra("datatype", 1);
			intent.putExtra("parsetype",3);
			startActivity(intent);
			ChooseStockFragment.this.finish();
		}
	};
	private void notifySearch(String key,Stock stock){ 
		GridStocksFragment fg = (GridStocksFragment)mAdapter.instantiateItem(vp,position);
		//GridStocksFragment fg = (GridStocksFragment) mAdapter.getItem(this.position);//GridStocksFragment.newInstance(this.position); 
		Bundle args = fg.getArguments(); 
		args.putString("key", key); 
		args.putString("name", stock.name);
		args.putString("code", stock.code);
		args.putString("ncode", stock.getNcode()); 
		fg.searchLoad();
	}

	@Override
	public void onPageScrollStateChanged(int arg0) { 
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) { 
		
	}

	@Override
	public void onPageSelected(int position) {  
		this.position = position;
		chooseTitle.setText(tabs[position]);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) { 
		if(keyCode == KeyEvent.KEYCODE_BACK){
			if(lv_search.getVisibility() == View.VISIBLE){
				lv_search.setVisibility(View.GONE);
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}
}
